package com.tutu.controller.backend;

import com.tutu.common.DataTableFactory;
import com.tutu.controller.common.CommonController;
import com.tutu.service.FunctionalModuleService;
import com.tutu.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能模块管理
 */
@Controller
@RequestMapping(value = "backend/functionalModule")
public class FunctionalModuleController extends CommonController {

    public static Map<String, Map<String, Object>> functionalModuleMap = new HashMap<>();

    @Autowired
    FunctionalModuleService functionalModuleService;

    @RequestMapping(value = "/index")
    public String index() {
        return "backend/functionalModule/list";
    }

    @RequestMapping(value = "/child/index")
    public String childIndex(Model model, Integer id) {
        model.addAttribute("functionalModule", functionalModuleMap.get(id + ""));

        return "backend/functionalModule/childList";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response, Integer type, String keyWord, Integer draw, Integer start, Integer length) {
        int pageNum = getPageNum(start, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, functionalModuleService.pageByParams(type, keyWord, pageNum, length));

        WebUtil.printJson(response, result);
    }

    @RequestMapping("/child/list")
    public void childList(HttpServletResponse response, Integer functionalModuleId, String keyWord, Integer type, Integer draw, Integer start, Integer length) {
        int pageNum = getPageNum(start, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, functionalModuleService.pageChildByParams(functionalModuleId, keyWord, type, pageNum, length));

        WebUtil.printJson(response, result);
    }

    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> saveInfo(HttpServletRequest request, Integer id, Integer type, String name) {
        return functionalModuleService.saveInfo(request, id, type, name);
    }

    @RequestMapping("/del")
    @ResponseBody
    public Map<String, Object> delete(HttpServletRequest request, Integer id) {
        return functionalModuleService.deleteById(request, id);
    }

    @RequestMapping("/updateCompany")
    @ResponseBody
    public Map<String, Object> updateCompany(HttpServletRequest request, Integer functionalModuleId, Integer companyId, Integer type) {
        return functionalModuleService.updateInfo(request, functionalModuleId, companyId, type);
    }
}
